<!DOCTYPE html>
<html lang="GB18030" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" th:href="@{/css/font-awesome.min.css}" href="css/font-awesome.min.css">
    <link rel="stylesheet" th:href="@{/css/main.css}" href="css/main.css">
    <link rel="stylesheet" th:href="@{/css/doc.min.css}" href="css/doc.min.css">
    <link rel="stylesheet" href="/ztree/zTreeStyle.css">
    <style>
        .tree li {
            list-style-type: none;
            cursor: pointer;
        }
    </style>
</head>

<body>

<div th:replace="common/top::navigation('许可维护')"></div>
<div class="container-fluid">
    <div class="row">

        <div th:replace="common/left::leftsidebar('/permission/permissionPage/')"></div>

        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading"><i class="glyphicon glyphicon-th-list"></i> 权限菜单列表
                    <div style="float:right;cursor:pointer;" data-toggle="modal" data-target="#myModal"><i
                            class="glyphicon glyphicon-question-sign"></i></div>
                </div>
                <div class="panel-body">
                    <ul id="permisssionTree" class="ztree"></ul>
                </div>
            </div>
        </div>

    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">帮助</h4>
            </div>
            <div class="modal-body">
                <div class="bs-callout bs-callout-info">
                    <h4>没有默认类</h4>
                    <p>警告框没有默认类，只有基类和修饰类。默认的灰色警告框并没有多少意义。所以您要使用一种有意义的警告类。目前提供了成功、消息、警告或危险。</p>
                </div>
                <div class="bs-callout bs-callout-info">
                    <h4>没有默认类</h4>
                    <p>警告框没有默认类，只有基类和修饰类。默认的灰色警告框并没有多少意义。所以您要使用一种有意义的警告类。目前提供了成功、消息、警告或危险。</p>
                </div>
            </div>
            <!--
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary">Save changes</button>
            </div>
            -->
        </div>
    </div>
</div>
<script th:src="@{/webjars/jquery/3.3.1/jquery.js}" src="jquery/jquery-2.1.1.min.js"></script>
<script th:src="@{/webjars/bootstrap/3.3.7/js/bootstrap.js}" src="bootstrap/js/bootstrap.min.js"></script>
<script th:src="@{/script/docs.min.js}" src="script/docs.min.js"></script>
<script src="/ztree/jquery.ztree.all-3.5.min.js"></script>
<script th:src="@{/webjars/github-com-sentsin-layer/3.0.3/src/layer.js}"></script>
<script type="text/javascript">
    $(function () {
        $(".list-group-item").click(function () {
            if ($(this).find("ul")) {
                $(this).toggleClass("tree-closed");
                if ($(this).hasClass("tree-closed")) {
                    $("ul", this).hide("fast");
                } else {
                    $("ul", this).show("fast");
                }
            }
        });

        var zNodes =
            [
                {
                    name: '超级父亲节点',
                    children: [{
                        name: '父节点', open: true, iconOpen: '/ztree/img/diy/1_open.png',
                        iconClose: "/ztree/img/diy/1_close.png",
                        children: [{name: '子节点', open: true}, {name: '子节点2', open: false}]
                    },
                        {
                            name: '父节点2', open: true,
                            children: [{name: '子节点', open: true}]

                        }]
                },
            ]


        var setting = {
            view: {
                selectedMulti: false,
                addDiyDom: function (treeId, treeNode) {
                    var icoObj = $("#" + treeNode.tId + "_ico"); // tId = permissionTree_1, $("#permissionTree_1_ico")
                    if (treeNode.iconClass) {
                        icoObj.removeClass("button ico_docu ico_open").addClass(treeNode.iconClass).css("background", "");
                    }
                },

                addHoverDom: addHoverDom,
                removeHoverDom: removeHoverDom
            },
            async: {
                enable: true,
                url: "/permission/queryAllPermission",
                autoParam: ["id", "name=n", "level=lv"]
            }
        };

        $.fn.zTree.init($("#permisssionTree"), setting);


        function addHoverDom(treeId, treeNode) {
            //   <a><span></span></a>
            var aObj = $("#" + treeNode.tId + "_a"); // tId = permissionTree_1, ==> $("#permissionTree_1_a")
            aObj.attr("href", "javascript:;");
            if (treeNode.editNameFlag || $("#btnGroup" + treeNode.tId).length > 0) return;
            var s = '<span id="btnGroup' + treeNode.tId + '">';
            if (treeNode.level == 0) {
                s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode(' + treeNode.id + ')" href="#" >&nbsp;&nbsp;<i class="fa fa-fw fa-plus rbg "></i></a>';
            } else if (treeNode.level == 1) {
                s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode(' + treeNode.id + ')" href="#" title="修改权限信息">&nbsp;&nbsp;<i class="fa fa-fw fa-edit rbg "></i></a>';
                if (treeNode.children.length == 0) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode(' + treeNode.id + ')" href="#" >&nbsp;&nbsp;<i class="fa fa-fw fa-times rbg "></i></a>';
                }
                s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode(' + treeNode.id + ')" href="#" >&nbsp;&nbsp;<i class="fa fa-fw fa-plus rbg "></i></a>';
            } else if (treeNode.level == 2) {
                s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode(' + treeNode.id + ')" href="#" title="修改权限信息">&nbsp;&nbsp;<i class="fa fa-fw fa-edit rbg "></i></a>';
                s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode(' + treeNode.id + ')" href="#">&nbsp;&nbsp;<i class="fa fa-fw fa-times rbg "></i></a>';
            }

            s += '</span>';
            aObj.after(s);
        }

        function removeHoverDom(treeId, treeNode) {
            $("#btnGroup" + treeNode.tId).remove();
        }


    });


    function addNode(pid) {
        window.location.href = "/permission/permissionAddPage/" + pid;
    }


    function editNode(id) {
        window.location.href = "/permission/permissionEditPage/" + id;
    }

    function deleteNode(id) {
        var index;
        layer.confirm("确认要删除", {icon: 3, title: '提示'}, function (index2) {
            layer.close(index2);
            $.ajax({
                type: "post",
                url: "/permission/delPermission/"+ id,
                data: {
                    _method: "DELETE",
                },
                beforeSend: function () {
                    index = layer.load();
                },
                success: function (result) {
                    layer.close(index);
                    if (result) {
                        layer.msg("删除成功", {time: 1000}, function () {
                            window.location.href = "/permission/permissionPage"
                        });
                    } else {
                        layer.msg("删除失败", {time: 1000}, function () {
                            layer.close(index);
                        });
                    }
                }
            });
        }, function (index) {
            layer.close(index);
        })
    }

</script>
</body>
</html>
